package com.tte.common.utils.office;

import com.spire.doc.Document;
import com.spire.doc.DocumentObject;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.HeaderFooter;
import com.spire.doc.documents.Paragraph;

import java.io.File;
import java.io.IOException;

/**
 * 付费，最多25个
 */
public class WordBySpire {

    public static String tempFilePath = "D:\\temp\\模板.docx";
    public static String newFilePath = "D:\\temp\\模板1.docx";
    public static String newFilePath2 = "D:\\temp\\模板2.docx";

    public static void main(String[] args) throws IOException {
        File file = new File(newFilePath); // 设置文件路径
        if (file.exists()) { // 文件存在
            file.delete();
        }
        file.createNewFile();
        copyForTemplate(tempFilePath, newFilePath);
    }

    //复制整篇文档内容
    public static void copyForTemplate(String templatePath, String newFilePath) {
        //加载文档1
        Document templateFile = new Document();
        templateFile.loadFromFile(templatePath);
        //加载文档2
        Document newFile = new Document();
        newFile.loadFromFile(newFilePath);
        //section.getBody().getChildObjects().getDocument().getText()获取所有文本
        //遍历文档1中的所有子对象
        for (int x = 0; x <300; x++) {
            for (int i = 0; i < templateFile.getSections().getCount(); i++) {
                Section section = templateFile.getSections().get(i);
                for (int j = 0; j < section.getBody().getChildObjects().getCount(); j++) {
                    Object object = section.getBody().getChildObjects().get(j);//复制文档1中的正文内容添加到文档2
                    newFile.getSections().get(0).getBody().getChildObjects().add(((DocumentObject) object).deepClone());
                }
            }
        }

//        //测试
//        Section section = templateFile.getSections().get(0);
//        section.getBody().getChildObjects().getDocument().getText().replaceFirst("列名", "中文名");
        //保存文档2
        newFile.saveToFile(newFilePath, FileFormat.Docx_2013);
        newFile.dispose();
    }

    //复制指定段落内容
    public static void copyBy(String templatePath, String newFilePath) {
        //加载文档1
        Document docTemplate = new Document();
        docTemplate.loadFromFile(templatePath);
        //获取文档1中的第三段
        Section sectionTemp = docTemplate.getSections().get(0);
        Paragraph paragraphTemp = sectionTemp.getParagraphs().get(2);
        //加载文档2，获取section
        Document doc2 = new Document();
        doc2.loadFromFile(newFilePath);
        Section section2 = doc2.getSections().get(0);
        //复制文档1中段落，添加到文档2
        Paragraph newparagraph = (Paragraph) paragraphTemp.deepClone();
        section2.getParagraphs().add(newparagraph);
        //保存文档2
        doc2.saveToFile(newFilePath2, FileFormat.Docx_2013);
        doc2.dispose();
    }

    //复制水印
    public void CopyWatermark(String templatePath, String newFilePath) {
        //加载文档1
        Document doc1 = new Document();
        doc1.loadFromFile(templatePath);
        //加载文档2
        Document doc2 = new Document();
        doc2.loadFromFile(newFilePath);
        //获取文档1的水印效果，设置到文档2
        doc2.setWatermark(doc1.getWatermark());
        //保存文档2
        doc2.saveToFile(newFilePath2, FileFormat.Docx_2013);
        doc2.dispose();
    }

    //复制Word页眉页脚
    public void CopyHeaderFooter(String templatePath, String newFilePath) {
        //加载文档1
        Document doc1 = new Document();
        doc1.loadFromFile(templatePath);
        //获取section
        Section section1 = doc1.getSections().get(0);
        //获取文档1的页眉页脚
        HeaderFooter header = section1.getHeadersFooters().getHeader();
        HeaderFooter footer = section1.getHeadersFooters().getFooter();
        //加载文档2
        Document doc2 = new Document();
        doc2.loadFromFile(newFilePath);
        //遍历文档2的section
        for (int i = 0; i < doc2.getSections().getCount(); i++) {
            Section section2 = doc2.getSections().get(i);
            //遍历页眉中的对象
            for (int j = 0; j < header.getChildObjects().getCount(); j++) {
                //获取页眉中的所有子对象
                Object object1 = header.getChildObjects().get(j);
                //复制文档1的页眉添加到文档2
                section2.getHeadersFooters().getHeader().getChildObjects().add(((DocumentObject) object1).deepClone());
            }
            //同理复制页脚
            for (int z = 0; z < footer.getChildObjects().getCount(); z++) {
                Object object2 = footer.getChildObjects().get(z);
                section2.getHeadersFooters().getFooter().getChildObjects().add(((DocumentObject) object2).deepClone());
            }
        }
        //保存文档2
        doc2.saveToFile(newFilePath2, FileFormat.Docx_2013);
        doc2.dispose();

    }


}
